package com.example.apione.controller.admin;

import com.example.apione.dao.ApiAccesslogDao;
import com.example.apione.dao.ApiInfoDao;
import com.example.apione.dao.WebInfoDao;
import com.example.apione.entity.ApiAccesslog;
import com.example.apione.entity.WebInfo;
import com.example.apione.model.LogModel;
import com.example.apione.model.Webinfomodel;
import com.example.apione.model.countApimodel;
import com.example.apione.utils.ResponseResult;
import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author 尘落
 * @date 2023/2/24
 */
@Data
@RestController
public class getAdminInfo {
    private final ApiInfoDao apiInfoDao;
    private final ApiAccesslogDao apiAccesslogDao;
    private final WebInfoDao webInfoDao;
    @GetMapping("/api/admininfo")
    public ResponseResult<HashMap<String, Object>> admininfo() {
        int[] apicountdata = new int[5];
        int[] daydata = new int[5];
        LinkedHashMap<String, Object> hashMap = new LinkedHashMap<>();
        LocalDateTime localDate = LocalDateTime.now();
        for (int i = 0; i < 5; i++) {
            daydata[4-i] = localDate.getDayOfMonth() - i;
        }
        HashMap<String, Integer> apicount = countApimodel.countApi(apiInfoDao);
        HashMap<String, Integer> aapiaccess = null;
        for (int i = 0; i < 5; i++) {
            aapiaccess = countApimodel.countAccess(apiAccesslogDao, localDate.minusDays(4-i).with(LocalTime.MIN).
                    toEpochSecond(ZoneOffset.ofHours(8)),localDate.minusDays(4-i).with(LocalTime.MAX).
                    toEpochSecond(ZoneOffset.ofHours(8)));
            apicountdata[i] = aapiaccess.get("allcount");
        }
        hashMap.put("api", apicount.get("apisum"));
        hashMap.put("access", aapiaccess.get("apisum"));
        hashMap.put("access_data", apicountdata);
        hashMap.put("access_time", daydata);
        hashMap.put("spider", apicount.get("apisum"));
        hashMap.put("spider_data", null);
        hashMap.put("spider_time", apicount.get("apisum"));
        return ResponseResult.ok(hashMap);
    }
    @GetMapping("/api/webinfo")
    public ResponseResult<WebInfo> getwebinfo(){
        return Webinfomodel.getinfo(webInfoDao);
    }
    @GetMapping("/api/setupinfo")
    public ResponseResult<WebInfo> upwebinfo(){
        return Webinfomodel.getinfo(webInfoDao);
    }

    @GetMapping("/api/getlog")
    public ResponseResult<List<ApiAccesslog>> getlog(){
        return LogModel.getlog(apiAccesslogDao);
    }
}
